﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Mechanics.GameEventMechanics {
    import flash.events.*;
    import Engine.Managers.*;
    import Mechanics.GameMechanicInterfaces.*;
    import Mechanics.*;
    import flash.utils.*;
    import Classes.*;
    import Classes.sim.*;
    import Classes.actions.*;
    import Mechanics.Transactions.*;

    public class MerchantCustomerMechanic implements IActionGameMechanic {

        protected static var m_transactionTimer:Timer;
        protected static var m_transactionList:Dictionary = new Dictionary();

        protected var m_owner:MechanicMapResource;
        protected var m_config:MechanicConfigData;

        private static function addAction(_arg1:MechanicMapResource, _arg2:String, _arg3:String, _arg4:int, _arg5:Boolean):void{
            if (m_transactionList[_arg1.getId()]){
                m_transactionList[_arg1.getId()].count = (m_transactionList[_arg1.getId()].count + _arg4);
                if (((_arg5) || ((m_transactionList[_arg1.getId()].count >= BusinessVisitBatchManager.MAX_NUMBER_OF_UPDATES)))){
                    onTransactionTmer(null);
                };
            } else {
                m_transactionList[_arg1.getId()] = {
                    count:_arg4,
                    owner:_arg1,
                    type:_arg2,
                    gameEvent:_arg3
                };
            };
            if (m_transactionTimer == null){
                m_transactionTimer = new Timer((BusinessVisitBatchManager.MAX_BATCH_DELAY_SECONDS * 1000));
                m_transactionTimer.addEventListener(TimerEvent.TIMER, onTransactionTmer);
                m_transactionTimer.start();
            };
        }
        private static function onTransactionTmer(_arg1:TimerEvent):void{
            var _local2:Object;
            if (!Global.isTransitioningWorld){
                for each (_local2 in m_transactionList) {
                    TransactionManager.addTransaction(new TMechanicAction(_local2.owner, _local2.type, _local2.gameEvent, {
                        operation:"process",
                        count:_local2.count
                    }));
                };
            };
            m_transactionList = null;
            m_transactionList = new Dictionary();
            if (m_transactionTimer != null){
                m_transactionTimer.removeEventListener(TimerEvent.TIMER, onTransactionTmer);
                m_transactionTimer.stop();
                m_transactionTimer = null;
            };
        }
        public static function forceFlush():void{
            onTransactionTmer(null);
        }

        public function hasOverrideForGameAction(_arg1:String):Boolean{
            return ((((_arg1 == PeepActionSelection.GAME_EVENT)) && (!(Global.isVisiting()))));
        }
        public function executeOverrideForGameEvent(_arg1:String, _arg2:Array=null):MechanicActionResult{
            var _local7:Peep;
            var _local3:Boolean;
            var _local4:Boolean;
            var _local5:Boolean;
            var _local6:Object = this.m_owner.getDataForMechanic(this.m_config.type);
            if (((((_local6) && (_local6.customersReq))) && ((_local6.customers < _local6.customersReq)))){
                _local6.customers++;
                _local3 = true;
                _local4 = true;
                _local7 = (_arg2[0] as Peep);
                if ((this.m_owner is IMerchant)){
                    (this.m_owner as IMerchant).crowdManager.makeNpcEnterMerchant(_local7);
                } else {
                    _local7.actionQueue.addActions(new ActionNavigateHotspots(_local7, this.m_owner), new ActionDie(_local7));
                };
                addAction(this.m_owner, this.m_config.type, _arg1, 1, (_local6.customers >= _local6.customersReq));
            } else {
                if (((((_local6) && (_local6.customersReq))) && ((_local6.customers >= _local6.customersReq)))){
                    forceFlush();
                };
            };
            this.m_owner.setDataForMechanic(this.m_config.type, _local6);
            return (new MechanicActionResult(_local3, _local4, false, {operation:"process"}));
        }
        public function initialize(_arg1:IMechanicUser, _arg2:MechanicConfigData):void{
            this.m_owner = (_arg1 as MechanicMapResource);
            this.m_config = _arg2;
        }
        public function isVisitEnabled():Boolean{
            return (false);
        }

    }
}//package Mechanics.GameEventMechanics 
